﻿<#
    Successfully installing a SharePoint 2013 environment.
    I was logged on with the SharePoint Farm Account which was in the local Administrators Group. 
	Both only for the purpose to successfully activate User Profile Synchronization. 
	After that I removed the account from the local Administrators Group.
    I restarted the server after putting the SharePoint Farm Account in the local Administrators 
	Group because of the security token to become active.
    I opened a PowerShell console using 'Run as administrator' to run the PowerShell script.
#>


#Create a new Service Application. Btw the name of the application pool is sufficient here: 
$ups = New-SPProfileServiceApplication –Name "User Profile Service" `
    -ApplicationPool "Default SharePoint Services App Pool"  `
    -ProfileDBServer "SP2010Server" -ProfileDBName "Profile_DB"  `
    -SocialDBServer "SP2010Server" -SocialDBName "Social_DB"  `
    -ProfileSyncDBServer "SP2010Server" -ProfileSyncDBName "Sync_DB"  `
    -MySiteHostLocation "http://sp2010server/sites/MySite"

#Create a service application proxy: 
New-SPProfileServiceApplicationProxy -Name "User Profile Proxy" –DefaultProxyGroup `
    -ServiceApplication $ups

#Get the instances of 2 services related to User Profile Synchronization from your SharePoint server 
#(usually APP) where you want to run User Profile Sync: 
$service1 = $(Get-SPServiceInstance | 
    where {$_.TypeName -match "User Profile Service" } | 
    where {$_.Server -match "SPServer Name=SP2010Server"})
$service2 = $(Get-SPServiceInstance | 
    where {$_.TypeName -match "User Profile Synchronization Service" } | 
    where {$_.Server -match "SPServer Name=SP2010Server"}) 

#Start the User Profile Service: 
Start-SPServiceInstance -Identity $service1.ID  

#Start the User Profile Synchronization Service: 
$ups.SetSynchronizationMachine("YOUR_APP_SERVERNAME", $service2.ID, 
    "domain\FarmAcc", "FarmAccPwd")
Start-SPServiceInstance -Identity $service2.ID 

